/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package com.dfdchem.imd.kaiwu.util;

import com.dfdchem.imd.kaiwu.interfaces.OnHttpResponseListener;
import com.dfdchem.imd.kaiwu.interfaces.impl.HttpResponseListenerImpl;
import com.dfdchem.imd.kaiwu.manager.HttpManager;
import com.dfdchem.imd.kaiwu.manager.IntranetManager;
import com.dfdchem.imd.kaiwu.model.AppPublish;
import com.dfdchem.imd.kaiwu.model.SysMessage;
import com.dfdchem.imd.kaiwu.model.User;
import com.dfdchem.imd.kaiwu.model.UserFeedback;

import java.util.HashMap;
import java.util.Map;


/**HTTP请求工具类
 * @author Lemon
 * @use 添加请求方法xxxMethod >> HttpRequest.xxxMethod(...)
 * @must 所有请求的url、请求方法(GET, POST等)、请求参数(key-value方式，必要key一定要加，没提供的key不要加，value要符合指定范围)
 *       都要符合后端给的接口文档
 */
public class RequestUtil {

	/**基础URL，这里服务器设置可切换*/
	public static String URL_BASE_API = IntranetManager.URL_BASE_API;
	public static String URL_BASE_ADM = IntranetManager.URL_BASE_ADM;
	public static String URL_HELP = IntranetManager.URL_HELP;
	public static final String PAGE = "page";
	public static final String LIMIT = "limit";

	public static String url_get_captcha = URL_BASE_ADM + "/Api/Captcha/Get";
	public static String url_staff_register = URL_BASE_ADM + "/Api/Captcha/Verify";
	public static String url_login = URL_BASE_API + "/Fe/User/Login";
	public static String url_user_info = URL_BASE_API + "/Fe/User/Info";
	public static String url_version_check = URL_BASE_API + "/Fe/AppVersion/CheckUpdate";
	public static String url_get_module = URL_BASE_API + "/Fe/Module/Index";
	public static String url_get_banner = URL_BASE_API + "/Fe/Banner/Index";
	public static String url_document = URL_BASE_API + "/Fe/Document";
	public static String url_get_config = URL_BASE_API + "/Fe/AppConfig/Search?key=";
	public static String url_get_launch_ad = URL_BASE_API + "/Fe/AppConfig/Search?key=LaunchAd";
	public static String url_file_upload = URL_BASE_API + "/Fe/Upload/Index?bucket=public&path=app";
	public static String url_file = URL_BASE_API + "/Fe/Files/Index";
	public static String url_file_short = "/Fe/Files/Index";
	public static String url_user_feedback = URL_BASE_ADM + "/Api/UserFeedback/Add";
	public static String url_get_article_list = URL_BASE_API + "/Fe/AppArticle/Index/";
	public static String url_get_article = URL_BASE_API + "/Fe/AppArticle/Index/";
	public static String url_get_alarm_count = URL_BASE_ADM + "/Api/Alarm/CountAlarmByFUserId?FUserId=";
	public static String url_get_alarm_list = URL_BASE_ADM + "/Api/AlarmCenter/GetList";
	public static String url_user_info_modify = URL_BASE_API + "/Fe/User/InfoModify";
	public static String url_get_message = URL_BASE_API + "/Fe/Message/GetMessage";
	public static String url_get_sys_message = URL_BASE_API + "/Fe/Message/GetSysMessageList";
	public static String url_get_sys_message_line = URL_BASE_API + "/Fe/Message/GetSysMessageLine";
	public static String url_get_user_message = URL_BASE_API + "/Fe/Message/GetUserMessageList";
	public static String url_get_user_message_line = URL_BASE_API + "/Fe/Message/GetUserMessageLine";
	public static String url_set_user_message_read = URL_BASE_API + "/Fe/Message/setUserMessageRead";
	public static String url_set_sys_message_read = URL_BASE_API + "/Fe/Message/setSysMessageRead";
	public static String url_get_user_list = URL_BASE_API + "/Fe/User/List";
	public static String url_send_message = URL_BASE_API + "/Fe/Message/Send";


	/**
	 * 获取配置信息
	 */
	/**注册
	 */
	public static void getConfig(final String key, final int requestCode, final OnHttpResponseListener listener) {
		HttpManager.getInstance().get(null, url_get_config + key, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**
	 * 获取启动广告
	 */
	/**注册
	 */
	public static void getLaunchAd(final int requestCode, final OnHttpResponseListener listener) {

		HttpManager.getInstance().get(null, url_get_launch_ad, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**注册
	 */
	public static void getCaptcha(final String phone, final int requestCode, final OnHttpResponseListener listener) {
		Map<String, Object> request = new HashMap<>();

		request.put("Phone", phone);
		HttpManager.getInstance().post(request, url_get_captcha, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**注册
	 */
	public static void register(final User user, final int requestCode, final OnHttpResponseListener listener) {

		Map<String, Object> request = new HashMap<>();

		request.put("Phone", user.getPhone());
		request.put("Imei", user.getPhone());
		request.put("EmpCardNo", user.getEmpCardNo());
		request.put("Code", user.getCode());
		request.put("Password", user.getPassword());

		HttpManager.getInstance().post(request, url_staff_register, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**登陆
	 * @param User user
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void login(final User user, final int requestCode, final OnHttpResponseListener listener) {

		Map<String, Object> request = new HashMap<>();

		request.put("Phone", user.getPhone());
		request.put("Imei", user.getPhone());
		request.put("Password", user.getPassword());

		HttpManager.getInstance().post(request, url_login, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**获取用户信息
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void getUserInfo(final int requestCode, final OnHttpResponseListener listener) {
		HttpManager.getInstance().get(null, url_user_info, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**版本更新检测
	 * @param AppPublish appPublish
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void versionCheck(final AppPublish appPublish, final int requestCode, final OnHttpResponseListener listener) {
		Map<String, Object> request = new HashMap<>();

		request.put("packageName", appPublish.getPackageName());
		request.put("platform", appPublish.getPlatform());
		request.put("versionCode", appPublish.getVersionCode());

		HttpManager.getInstance().get(request, url_version_check, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**获取首页Banner
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void getBanner(final int requestCode, final OnHttpResponseListener listener) {
		HttpManager.getInstance().get(null, url_get_banner, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**获取首页功能入口
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void getModule(final int requestCode, final OnHttpResponseListener listener) {
		HttpManager.getInstance().get(null, url_get_module, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**获取文章列表
	 * @param int page
	 * @param int limit
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void getArticleList(final int page, final int limit, final int requestCode, final OnHttpResponseListener listener) {
		HttpManager.getInstance().get(null, url_get_article_list + "?page="+page + "&limit="+limit, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**获取文章详情
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void getArticle(final int articleId, final int requestCode, final OnHttpResponseListener listener) {
		HttpManager.getInstance().get(null, url_get_article + "/" + articleId, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**获取报警数量
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void getAlarmCount(final long user_id, final int requestCode, final OnHttpResponseListener listener) {
		HttpManager.getInstance().get(null, url_get_alarm_count + user_id, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**获取报警信息
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void getAlarmList(final int requestCode, final OnHttpResponseListener listener) {
		HttpManager.getInstance().get(null, url_get_alarm_list, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**获取静态页信息
	 * @param String key
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void getDocumentByKey(final String key, final int requestCode, final OnHttpResponseListener listener) {
		HttpManager.getInstance().get(null, url_document + "/" + key, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**用户反馈
	 * @param UserFeedback userFeedback
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void feedback(final UserFeedback userFeedback, final int requestCode, final OnHttpResponseListener listener) {
		Map<String, Object> request = new HashMap<>();
		request.put("Title", userFeedback.getTitle());
		request.put("Content", userFeedback.getContent());
		request.put("Name", userFeedback.getName());
		request.put("PhoneNo", userFeedback.getPhoneNo());
		request.put("AttachmentList", userFeedback.getAttachmentList());
		HttpManager.getInstance().post(request, url_user_feedback, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**用户信息修改，目前只支持修改头像
	 * @param User user
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void user_info_modify(final User user, final int requestCode, final OnHttpResponseListener listener) {
		Map<String, Object> request = new HashMap<>();
		request.put("Avatar", user.getAvatar());
		HttpManager.getInstance().post(request, url_user_info_modify, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**获取消息
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void get_message(final int requestCode, final OnHttpResponseListener listener) {
		HttpManager.getInstance().get(null, url_get_message, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**获取系统消息
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void get_sys_message(final int requestCode, final OnHttpResponseListener listener) {
		HttpManager.getInstance().get(null, url_get_sys_message, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**获取用户消息
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void get_user_message(final int requestCode, final OnHttpResponseListener listener) {
		HttpManager.getInstance().get(null, url_get_user_message, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**获取系统消息线
	 * @param long from_user_id
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void get_sys_message_list(final long from_user_id, final int requestCode, final OnHttpResponseListener listener) {
		HttpManager.getInstance().get(null, url_get_sys_message_line + "?from=" + from_user_id + "&page=1&limit=100", requestCode, new HttpResponseListenerImpl(listener));
	}

	/**获取用户消息线
	 * @param long from_user_id
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void get_user_message_list(final long from_user_id, final int requestCode, final OnHttpResponseListener listener) {
		HttpManager.getInstance().get(null, url_get_user_message_line + "?from=" + from_user_id + "&page=1&limit=100", requestCode, new HttpResponseListenerImpl(listener));
	}

	/**获取用户列表
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void get_user_list(final int page, final int limit, final int requestCode, final OnHttpResponseListener listener) {
		HttpManager.getInstance().get(null, url_get_user_list + "?page="+page+"&limit="+limit, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**设置消息已读
	 * @param int from_user_id
	 * @param int message_id
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void set_message_read(final int message_type, final long from_user_id, final long message_id, final int requestCode, final OnHttpResponseListener listener) {
		Map<String, Object> request = new HashMap<>();
		request.put("from", from_user_id);
		request.put("message_id", message_id);
		HttpManager.getInstance().post(request, message_type == 1 ? url_set_sys_message_read : url_set_user_message_read, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**发送消息
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void send_message(final SysMessage userMessage, final int requestCode, final OnHttpResponseListener listener) {
		Map<String, Object> request = new HashMap<>();
		request.put("From", userMessage.getFrom());
		request.put("To", userMessage.getTo());
		request.put("Status", 1);
		request.put("Content", userMessage.getContent());
		request.put("ContentType", userMessage.getContentType());
		HttpManager.getInstance().post(request, url_send_message, requestCode, new HttpResponseListenerImpl(listener));
	}

	/**上传文件
	 * @param String fileName
	 * @param String mimeType
	 * @param String filePath
	 * @param int requestCode
	 * @param OnHttpResponseListener listener
	 * @param listener
	 */
	public static void uploadFile(final String fileName, final String mimeType, final String filePath, final int requestCode, final OnHttpResponseListener listener) {
		HttpManager.getInstance().uploadFile(fileName, mimeType, filePath, url_file_upload, requestCode, new HttpResponseListenerImpl(listener));
	}


	//account>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>



}